import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'lib_bt_platform_interface.dart';
import 'listener/OnBleConnectListener.dart';
import 'listener/OnBleDataListener.dart';
import 'listener/OnBleScanListener.dart';
import 'listener/OnOtaListener.dart';

/// An implementation of [LibBtPlatform] that uses method channels.
class MethodChannelLibBt extends LibBtPlatform {
  OnBleConnectListener? _listener;

  OnBleScanListener? _scanListener;
  OnOtaListener? _otaListener;
  OnBleDataListener? _dataListener;

  //设置蓝牙连接回调
  @override
  void setOnBleConnectListener(OnBleConnectListener listener) {
    _listener = listener;
  }

  @override
  void setOnBleScanListener(OnBleScanListener listener) {
    _scanListener = listener;
  }
  @override
  void setOnBleDataListener(OnBleDataListener listener) {
    _dataListener = listener;
  }

  @override
  void setOnOtaListener(OnOtaListener listener) {
    _otaListener = listener;
  }

  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('lib_bt');

  MethodChannelLibBt() {
    methodChannel.setMethodCallHandler((call) async {
      switch (call.method) {
        case "onStartScan":
          _scanListener?.onStartScan();
          break;
        case "onStopScan":
          _scanListener?.onStopScan();
          break;
        case "onScanResult":
          _scanListener?.onScanResult(call.arguments);
          break;
        case "onConnecting":
          _listener?.onStartConnect(call.arguments);
          break;
        case "onConnectSuccess":
          _listener?.onConnected(call.arguments);
          break;
        case "onDisConnect":
        case "onConnectFailed":
          _listener?.onDisconnected(call.arguments);
          break;
        case "onMtuChanged":
          _listener?.onMtuChanged(call.arguments["address"], call.arguments["mtu"]);
          break;
        case "onData":
          _dataListener?.onDataReceived(call.arguments["address"], call.arguments["uuid"], call.arguments["data"]);
          break;

        ///ota相关
        case "onOtaStart":
          _otaListener?.onOtaStart();
          break;
        case "onOtaBootLoaderProgress":
          _otaListener?.onOtaBootLoaderProgress(call.arguments);
          break;
        case "onOtaProgress":
          _otaListener?.onOtaProgress(call.arguments);
          break;
        case "onOtaSuccess":
          _otaListener?.onOtaSuccess();
          break;
        case "onOtaFail":
          _otaListener?.onOtaFailed("${call.arguments ?? "未知错误"}");
          break;
        case "onOtaAvailable":
          _otaListener?.onOtaAvailable(
              call.arguments["address"], call.arguments["mac"], call.arguments["isMandatoryUpgrade"]);
          break;
        default:
      }
    });
  }

  @override
  Future<T?> invokeMethod<T>(String method, [dynamic arguments]) {
    return methodChannel.invokeMethod<T>(method, arguments);
  }

  @override
  Future<String?> getPlatformVersion() async {
    final version = await invokeMethod<String>('getPlatformVersion');
    return version;
  }
}
